package com.whmall.service.crm.module.supplierreview.impl;

import cn.hutool.core.bean.BeanUtil;
import com.whmall.service.crm.entity.dto.supplierreview.SupplierUserReviewTagDTO;
import com.whmall.service.crm.module.supplierreview.SupplierUserReviewTagManager;
import com.whmall.service.crm.module.supplierreview.entity.SupplierUserReviewTagVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author qiao shengda
 * @since 2021/6/23
 */
@Service
public class SupplierUserReviewTagManagerImpl implements SupplierUserReviewTagManager {

    private final static String KEY = "$dict$supplier_user_review_tag";
    private RedisTemplate<String, SupplierUserReviewTagVO> redisTemplate;

    @Override
    public Map<String, List<SupplierUserReviewTagDTO>> findAll() {
        Map<Integer, SupplierUserReviewTagVO> entries = hashOperators().entries();
        if (null == entries) {
            return Collections.emptyMap();
        }
        List<Map.Entry<Integer, SupplierUserReviewTagVO>> temp = entries.entrySet().stream().filter(item -> item.getValue().getIsEnabled() == 1).collect(Collectors.toList());
        Map<String, List<SupplierUserReviewTagDTO>> result = new HashMap<>(2);
        ArrayList<SupplierUserReviewTagDTO> good = new ArrayList<>();
        ArrayList<SupplierUserReviewTagDTO> bad = new ArrayList<>();
        for (Map.Entry<Integer, SupplierUserReviewTagVO> item : temp) {
            SupplierUserReviewTagDTO value = new SupplierUserReviewTagDTO();
            if (item.getValue().getIsEnabled().equals(1)){
                value.setId(item.getKey());
                value.setName(item.getValue().getName());
                if (item.getValue().getIsGood() == 1){
                    good.add(value);
                }else {
                    bad.add(value);
                }
            }
        }

        result.put("good", good);
        result.put("bad", bad);
        return result;
    }

    @Override
    public List<SupplierUserReviewTagDTO> findList() {
        Map<Integer, SupplierUserReviewTagVO> entries = hashOperators().entries();
        if (null == entries) {
            return Collections.emptyList();
        }
        List<SupplierUserReviewTagDTO> result = new ArrayList<>();
        List<Map.Entry<Integer, SupplierUserReviewTagVO>> temp = entries.entrySet().stream().filter(item -> item.getValue().getIsEnabled() == 1).collect(Collectors.toList());
        for (Map.Entry<Integer, SupplierUserReviewTagVO> item : temp) {
            SupplierUserReviewTagDTO value = new SupplierUserReviewTagDTO();
            if (item.getValue().getIsEnabled().equals(1)){
                value.setId(item.getKey());
                value.setName(item.getValue().getName());
                result.add(value);
            }
        }
        return result;
    }

    private BoundHashOperations<String, Integer, SupplierUserReviewTagVO> hashOperators() {
        return redisTemplate.boundHashOps(KEY);
    }

    @Autowired
    public SupplierUserReviewTagManagerImpl(RedisTemplate<String, SupplierUserReviewTagVO> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

}
